package com.rfid.assistant.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListActivity <T extends BaseAdapter,Y,G extends BasePresenter> extends BaseActivity<G> implements Listener<Y>  {

    protected RecyclerView mRecyclerView;
    protected T mAdapter;
    protected Listener<Y> mListener;
    protected List<Y> mData = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        mAdapter = getAdapter();
        mAdapter.setListener(this);
        mRecyclerView = findViewById(getRecyclerViewId());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mAdapter);
    }

    protected abstract int getLayoutResId();

    protected abstract T getAdapter();

    protected abstract int getRecyclerViewId();


    public void setData(List<Y> books) {
        if (books != mData) {
            System.out.println("addAll > " +books);
            mData.clear();
            mData.addAll(books);
        }
        if (mAdapter != null) {
            System.out.println("notifyDataSetChanged");
            mAdapter.notifyDataSetChanged();
        }
    }

}
